#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   BOOT_ECUReset_test.py
@Time    :   2025/03/20 16:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   Bootloader模式专用ECU复位测试
'''

from pytest_assume.plugin import assume

import pytest
import logging
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

'''物理寻址编程会话下专用测试数据'''
ECUReset_Valid = [
    ("11 01", "51 01"),          # 有效复位请求
    ("11 81", ""),               # 抑制响应复位
]

ECUReset_NRC13 = [
    ("11", "7F 11 13"),         # 缺失子功能
    ("11 01 00", "7F 11 13"),   # 带多余参数
    ("11 81 00", "7F 11 13"),   # 带多余参数
    ("11 81 00 11", "7F 11 13"),# 带多余参数
    ("11 01 00 11", "7F 11 13"),# 带多余参数
]

ECUReset_NRC12 = [
    ("11 00", "7F 11 12"),      # 无效子功能0x00
    ("11 02", "7F 11 12"),      # 无效子功能0x02
    ("11 03", "7F 11 12"),      # 无效子功能0x02
    ("11 04", "7F 11 12"),      # 无效子功能0x02
    ("11 05", "7F 11 12"),      # 无效子功能0x02
    ("11 80", "7F 11 12"),      # 无效子功能0x80
    ("11 82", "7F 11 12"),      # 无效子功能0x82
    ("11 83", "7F 11 12"),      # 无效子功能0x83
    ("11 84", "7F 11 12"),      # 无效子功能0x84
    ("11 85", "7F 11 12"),      # 无效子功能0x85
]

'''物理寻址编程会话下物理寻址测试'''
class Test_Programming_ECUReset_Phy(object):

    @allure.severity('critical')
    @allure.title('物理寻址编程会话下复位功能测试')
    @allure.description('验证编程会话下ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, _ in ECUReset_Valid])
    def test_ECUReset_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)  # 使用编程会话

    @allure.severity('normal')
    @allure.title('物理寻址编程会话下NRC13测试')
    @allure.description('验证编程会话下ECU硬复位请求长度不符返回NRC13功能')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, _ in ECUReset_NRC13])
    def test_ECUReset_NRC13_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址编程会话下NRC12测试')
    @allure.description('验证编程会话下ECU硬复位请求子功能不符返回NRC12功能')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, _ in ECUReset_NRC12])
    def test_ECUReset_NRC12_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址编程会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC13和NRC12的优先级')
    def test_ECUReset_NRC12AndNRC13_priority_Programming_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '7F 11 12', 2, iStep, False)

'''功能寻址测试'''
class Test_Programming_ECUReset_Func(object):

    @allure.severity('critical')
    @allure.title('功能寻址编程会话下功能寻址复位测试')
    @allure.description('验证功能寻址下ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, _ in ECUReset_Valid])
    def test_ECUReset_Programming_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址编程会话下功能寻址NRC13测试')
    @allure.description('验证功能寻址下ECU硬复位请求长度不符返回NRC13功能')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, _ in ECUReset_NRC13])
    def test_ECUReset_NRC13_Programming_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址编程会话下功能寻址NRC12测试')
    @allure.description('验证功能寻址下ECU硬复位请求子功能不符返回NRC12功能')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, _ in ECUReset_NRC12])
    def test_ECUReset_NRC12_Programming_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址编程会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC13和NRC12的优先级')
    def test_ECUReset_NRC12AndNRC13_priority_Programming_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '', 2, iStep, False, 'UDSFun')

